package com.swanclouds.redfish.updates;

import com.google.common.base.Preconditions;
import com.swanclouds.api.updates.FirmwareInventoryService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.Payload;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.model.updates.FirmwareInventory;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseUpdateService;
import com.swanclouds.redfish.updates.domin.FirmwareInventoryModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:      HuangFuYuHui
 * Title:       FirmwareInventoryServiceImpl
 * Description: 查询服务器当前可升级的固件集合资源的信息服务
 * Date:        2022/7/27 17:43
 */
public class FirmwareInventoryServiceImpl extends BaseUpdateService implements FirmwareInventoryService {

    /**
     * 查询可升级固件集合资源信息
     * @return CollectionResource
     **/
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.PATH_UPDATESERVICE_FIRMWAREINVENTORY)).execute();
    }

    /**
     * 查询服务器指定的可升级固件资源的信息。
     * @param  firmwareInventoryId 可升级固件资源Id
     * @return  com.swanclouds.model.updates.FirmwareInventory
     **/
    @Override
    public FirmwareInventory getFirmwareInventory(String firmwareInventoryId) {
        Preconditions.checkNotNull(firmwareInventoryId, "firmwareInventoryId is null");
        return get(FirmwareInventoryModel.class,uri(ClientConstants.PATH_UPDATESERVICE_FIRMWAREINVENTORY_ID,firmwareInventoryId)).execute();
    }

    /**
     * 查询服务器当前可升级的固件集合资源的信息。
     * @return  java.util.List
     **/
    @Override
    public List<FirmwareInventoryModel> getFirmwareInventoryList() {
        List<FirmwareInventoryModel> firmwareInventoryModels = new ArrayList<>();
        CollectionResource collectionResource = get();
        collectionResource.getCollectionResourceList().stream().forEach(ref -> {
            FirmwareInventoryModel execute = get(FirmwareInventoryModel.class, uri(ref)).execute();
            firmwareInventoryModels.add(execute);
        });
        return firmwareInventoryModels;
    }

    /**
     * 文件上传
     * @param  payload 上传文件
     * @return  com.swanclouds.model.common.ActionResponse
     **/
    @Override
    public ActionResponse uploadFile(Payload payload) {
        return post(ActionResponse.class,uri(ClientConstants.PATH_UPDATESERVICE_FIRMWAREINVENTORY))
                .header(ClientConstants.HEADER_CONTENT_TYPE,ClientConstants.MULTIPART_FORM_DATA)
               /* .entity()*/
                .execute();
    }

}
